package com.wu.datastructure.stack;

/**
 * @version v1.0
 * @ProjectName learning-java
 * @ClassName ArrayStack
 * @Description TODO 栈的实现
 * @Author wudl
 * @Date 2020/12/2 17:32
 */

public class ArrayStack<E> implements Stack<E> {

    private ArraySk<E> arraySk;


    public ArrayStack(int capacity) {
        arraySk = new ArraySk<>(capacity);
    }
    public ArrayStack() {
        arraySk = new ArraySk<>();
    }


    @Override
    public int getSize() {
        return arraySk.getSize();
    }

    @Override
    public boolean isEmpty() {
        return arraySk.getEmpty();
    }

    @Override
    public void push(E e) {
        arraySk.addLast(e);
    }

    @Override
    public E pop() {
        return arraySk.removeLast();
    }

    @Override
    public E peek() {
        return arraySk.getLast();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("Stack: ");
        res.append('[');
        for (int i = 0; i < arraySk.getSize(); i++) {
            res.append(arraySk.get(i));
            if (i != arraySk.getSize() - 1) {
                res.append(", ");
            }
        }
        res.append("] top");
        return res.toString();
    }
}
